package com.ww.learn.coroutine

fun main() {
    val sequence = sequence {
        yield(1)
        yield(2)
        yield(3)
        yield(4)
        yieldAll(listOf<Int>(1, 2, 3, 4))
    }
    for (element in sequence) {
        println(element)
    }

    // 使用序列生成器实例实现斐波那契数列
    val fibonacci = sequence {
        yield(1L)
        var current = 1L
        var next = 1L
        while (true) {
            yield(next)
            next += current
            current = next - current
        }
    }

    fibonacci.take(10).forEach(::println)
}